<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue 学习 1-3</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
</head>

<body>
    <div id="app">
        <p>Original message: "{{ message }}"</p>
        <p>Computed reversed message: "{{ reversedMessage }}"</p>
    </div>
    <div id="demo">{{ fullName }}</div>
    <div id="watch-example">
        <p>
            问是/否问题：
            <input v-model="question">
        </p>
        <p>{{ answer }}</p>

    </div>
    <h2>Class 与 Style 绑定</h2>
    <div id="classStyle">
        <div class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }">显示出来了</div>
        <div class="static" v-bind:class="classObject">显示出来了，classObject</div>
        <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">这是内联样式</div>
        <div v-bind:style="styleObject">这是内联样式styleObject</div>
    </div>
</body>
<style>
    .static {
        display: none
    }

    .active {
        display: block;
    }

    .text-danger {
        color: red;
    }
</style>
<script>

    var vm = new Vue({
        el: '#app',
        data: {
            message: 'Hello'
        },
        computed: {
            // 计算属性的 getter
            reversedMessage: function () {
                // `this` 指向 vm 实例
                return this.message.split('').reverse().join('')
            }
        }
    })
    // 计算属性
    var demo = new Vue({
        el: '#demo',
        data: {
            firstName: 'Foo',
            lastName: 'Bar'
        },
        computed: {
            // fullName: function () {
            //     return this.firstName + ' ' + this.lastName
            // }
            //计算属性中有setter方法，set，当set被调用时，get也会被调用。
            fullName: {
                // getter
                get: function () {
                    // console.log("12212");
                    return this.firstName + ' ' + this.lastName
                },
                // setter
                set: function (newValue) {
                    // console.log(newValue);
                    var names = newValue.split(' ')
                    this.firstName = names[0]
                    this.lastName = names[names.length - 1]
                }
            }
        }

    })
    // 侦听器
    var watchExampleVM = new Vue({
        el: '#watch-example',
        data: {
            question: '',
            answer: '除非你问问题，否则我不能给你答案！',
        },
        watch: {
            // 如果 `question` 发生改变，这个函数就会运行
            question: function (newQuestion, oldQuestion) {
                this.answer = '正在等待您停止键入…'
                this.debouncedGetAnswer()
            }
        },
        created: function () {
            // `_.debounce` 是一个通过 Lodash 限制操作频率的函数。
            // 在这个例子中，我们希望限制访问 yesno.wtf/api 的频率
            // AJAX 请求直到用户输入完毕才会发出。想要了解更多关于
            // `_.debounce` 函数 (及其近亲 `_.throttle`) 的知识，
            // 请参考：https://lodash.com/docs#debounce
            this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
        },
        methods: {
            getAnswer: function () {
                if (this.question.indexOf('?') === -1) {
                    this.answer = '问题通常包含问号'
                    return
                }
                this.answer = '思考...'
                var vm = this
                axios.get('https://yesno.wtf/api')
                    .then(function (response) {
                        vm.answer = _.capitalize(response.data.answer)
                    })
                    .catch(function (error) {
                        vm.answer = '错误！无法访问API. ' + error
                    })
            }
        }
    })
    // 绑定 HTML Class
    // 绑定内联样式
    var classStyle = new Vue({
        el: '#classStyle',
        data: {
            isActive: true,
            hasError: true,
            // classObject: {
            //     active: true,
            //     'text-danger': false
            // },
            activeColor: 'red',
            fontSize: 30,
            error: null,
            styleObject: {
                color: 'red',
                fontSize: '13px'
            }
        },
        computed: {
            classObject: function () {
                return {
                    active: this.isActive && !this.error,
                    'text-danger': this.error && this.error.type === 'fatal'
                }
            }
        }

    })
</script>

</html>